package com.moji.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moji.entity.GdArea;
import com.moji.entity.vo.GdAreaVo;
import com.moji.mapper.GdAreaMapper;
import com.moji.service.GdAreaService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

/**
 * <p>
 * 省市县，三级联动 服务实现类
 * </p>
 *
 * @author mds
 * @since 2023-10-26
 */
@Service
public class GdAreaServiceImpl extends ServiceImpl<GdAreaMapper, GdArea> implements GdAreaService {


    private final String areaTreeRedis = "AREA_TREE_REDIS";

    @Override
    public List<GdAreaVo> findProvince() {

        LambdaQueryWrapper<GdArea> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(GdArea::getAreaType, 1);
        queryWrapper.orderByAsc(GdArea::getAreaId);

        List<GdArea> list = this.list(queryWrapper);

        ArrayList<GdAreaVo> gdAreaVos = new ArrayList<>();
        for (GdArea gdArea : list) {
            GdAreaVo gdAreaVo = new GdAreaVo();

            BeanUtils.copyProperties(gdArea, gdAreaVo);
            gdAreaVos.add(gdAreaVo);
        }

        return gdAreaVos;
    }

    @Override
    public List<GdAreaVo> findCity(String areaCode) {
        LambdaQueryWrapper<GdArea> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(GdArea::getAreaType, 2);
        if (!areaCode.equals("")) {
            queryWrapper.eq(GdArea::getAreaParent, areaCode);
        }
        queryWrapper.isNotNull(GdArea::getAreaParent);
        queryWrapper.orderByAsc(GdArea::getAreaId);


        List<GdArea> list = this.list(queryWrapper);
        ArrayList<GdAreaVo> gdAreaVos = new ArrayList<>();
        for (GdArea gdArea : list) {
            GdAreaVo gdAreaVo = new GdAreaVo();

            BeanUtils.copyProperties(gdArea, gdAreaVo);
            gdAreaVos.add(gdAreaVo);
        }

        return gdAreaVos;
    }

    @Override
    public List<GdAreaVo> findArea(String areaCode) {
        LambdaQueryWrapper<GdArea> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(GdArea::getAreaType, 3);
        queryWrapper.eq(GdArea::getAreaParent, areaCode);
        queryWrapper.isNotNull(GdArea::getAreaParent);
        queryWrapper.orderByAsc(GdArea::getAreaId);


        List<GdArea> list = this.list(queryWrapper);
        ArrayList<GdAreaVo> gdAreaVos = new ArrayList<>();
        for (GdArea gdArea : list) {
            GdAreaVo gdAreaVo = new GdAreaVo();

            BeanUtils.copyProperties(gdArea, gdAreaVo);
            gdAreaVos.add(gdAreaVo);
        }

        return gdAreaVos;
    }

    @Override
    public HashMap<String, String> findNameByCode(HashSet<String> areaCodes) {

        if (areaCodes == null || areaCodes.size() == 0) {
            return new HashMap<>();
        }
        LambdaQueryWrapper<GdArea> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(GdArea::getAreaCode, areaCodes);

        List<GdArea> list = this.list(queryWrapper);

        HashMap<String, String> stringStringHashMap = new HashMap<>();

        for (GdArea gdArea : list) {
            stringStringHashMap.put(gdArea.getAreaCode(), gdArea.getAreaName());
        }

        return stringStringHashMap;
    }

}
